﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ribbon.Core.Win32
{
    public class Caret
    {
        public Caret(int hwnd)
        {
            Win32Handle handle = new Win32Handle();
            handle.handle = new IntPtr(hwnd);
            this.Parent = handle;
        }

        public Caret(IntPtr hwnd)
        {
            Win32Handle handle = new Win32Handle();
            handle.handle = hwnd;
            this.Parent = handle;
        }

        public Caret(IWin32Window ctl)
        {
            this.Parent = ctl;
        }

        public Point Position
        {
            get
            {
                POINT p = new POINT();

                if (GetCaretPos(ref p))
                {
                    return new Point(p.x, p.y);
                }
                return Point.Empty;
            }
        }

        public Point GetPosition()
        {
            POINT p = new POINT();

            if (GetCaretPos(ref p))
            {
                return new Point(p.x, p.y);
            }

            return Point.Empty;
        }

        IWin32Window Parent = null;

        /// <summary>
        /// 创建光标
        /// </summary>
        /// <param name="hBitmap">图片句柄</param>
        /// <param name="nWidth">光标宽度</param>
        /// <param name="nHeight">光标长度</param>
        /// <returns>操作是否成功</returns>
        public bool Create(int hBitmap, int nWidth, int nHeight)
        {
            if (Parent == null)
            {
                return false;
            }
            else
            {
                return CreateCaret(Parent.Handle, hBitmap, nWidth, nHeight);
            }
        }

        /// <summary>
        /// 设置光标位置
        /// </summary>
        /// <param name="x">光标X坐标</param>
        /// <param name="y">光标Y坐标</param>
        /// <returns>操作是否成功</returns>
        public bool SetPos(int x, int y)
        {
            if (Parent == null)
            {
                return false;
            }
            else
            {
                return SetCaretPos(x, y);
            }
        }

        /// <summary>
        /// 设置光标位置
        /// </summary>
        /// <param name="p">坐标点</param>
        /// <returns>操作是否成功</returns>
        public bool SetPos(Point p)
        {
            if (Parent == null)
            {
                return false;
            }
            else
            {
                return SetCaretPos(p.X, p.Y);
            }
        }
        /// <summary>
        /// 隐藏光标
        /// </summary>
        /// <returns>操作是否成功</returns>
        public bool Hide()
        {
            if (Parent == null)
            {
                return false;
            }
            else
            {
                return HideCaret(Parent.Handle);
            }
        }
        /// <summary>
        /// 显示光标
        /// </summary>
        /// <returns>操作是否成功</returns>
        public bool Show()
        {
            if (Parent == null)
            {
                return false;
            }
            else
            {
                return ShowCaret(Parent.Handle);
            }
        }
         
        public  int GetBlinkTime()
        {
            if (Parent == null)
            {
                return 0;
            }
            else
            {
                return GetCaretBlinkTime();
            }
        }

        public bool SetBlinkTime(int seconds)
        {
            if (Parent == null)
            {
                return false ;
            }
            else
            {
                return SetCaretBlinkTime(seconds);
            }
        } 

        public bool Destroy()
        {
            if (Parent == null)
            {
                return false;
            }
            else
            {
                return DestroyCaret();
            }
        }

        private class Win32Handle : IWin32Window
        {
            public IntPtr handle = IntPtr.Zero;

            public IntPtr Handle
            {
                get { return handle; }
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct POINT
        {
            public int x;
            public int y;
        }

        //创建光标
        [DllImport("User32.dll")]
        private static extern bool CreateCaret(IntPtr hWnd, int hBitmap, int nWidth, int nHeight);
        //显示光标
        [DllImport("User32.dll")]
        private static extern bool ShowCaret(IntPtr hWnd);
        //隐藏光标
        [DllImport("User32.dll")]
        private static extern bool HideCaret(IntPtr hWnd);
        //设置光标位置
        [DllImport("User32.dll")]
        private static extern bool SetCaretPos(int x, int y);
        //销毁光标
        [DllImport("User32.dll")]
        private static extern bool DestroyCaret();
        //光标当前位置
        [DllImport("User32.dll")]
        private static extern bool GetCaretPos(ref  POINT p);

        [DllImport("User32.dll")]
        private static extern int GetCaretBlinkTime();

        [DllImport("User32.dll")]
        private static extern bool SetCaretBlinkTime(int seconds);
    }
}
